#define DEBUG
#include <cstdio>

using namespace std;

const int maxid=100000;

void insert(int next[], int prev[], int time[], long double rate[], int x) {
  int i;
  for (i=0; rate[next[i]]>rate[x] || (rate[next[i]]==rate[x] && time[next[i]]<time[x]); i=next[i]);
  prev[x] = i, prev[next[i]] = x;
  next[x] = next[i], next[i] = x;
}

void pop(int next[], int prev[], int x) {
  prev[next[x]] = prev[x], next[prev[x]] = next[x];
  prev[x] = next[x] = 0;
}

int main() {
  freopen("movie.in", "r", stdin);
  freopen("movie.out", "w", stdout);

  static int next[maxid+1], prev[maxid+1], time[maxid+1], actor[maxid+1];
  static long double rate[maxid+1];
  int n;
  scanf("%d", &n);
  rate[0] = -1;
  for (int i=1; i<=n; i++) {
    char opt;
    scanf(" %c ", &opt);
    if (opt=='Q') {
      int t;
      scanf("%d", &t);
      int p=0;
      for (int x=1; x<=t; x++, p=next[p]);
      printf("%d\n", p);
    } else if (opt=='R') {
      int id, x, last=maxid+1;
      scanf("%d %d", &id, &x);
      for (int j=1; j<=x; j++) {
	int a;
	scanf("%d", &a);
	if (time[last]<time[actor[a]]) {
	  last = actor[a];
	}
	actor[a] = id;
      }
      rate[id] = last ? rate[last] : 0;
      time[id] = i;

      insert(next, prev, time, rate, id);
    } else if (opt=='C') {
      int id, score;
      scanf("%d %d", &id, &score);
      pop(next, prev, id);
      rate[id] = (rate[id]+score)/2;
      insert(next, prev, time, rate, id);
    }
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
